/*/
 * 首页逻辑处理的js代码
 * By 张剑锋
/*/

let srch=null
let list=null

window.onload=()=>{
    initHeadOnload()
    srch=document.getElementById('srch')
    list=document.getElementById('list')
    let key=getUrlArgu().key
    console.log(key)
    if(!key)
        location.href='../home'
    srch.innerText=`搜索 ${key}`
    let data=new FormData()
    let xhr=new XMLHttpRequest()
    data.append('key',key)
    xhr.open('GET','url')
    // xhr.send(data)
    xhr.onload=()=>{
        if(xhr.status==200){}
    }

    addBooksInfo(testData)
}
function getUrlArgu(){
    let url=decodeURI(location.search)
    let arg=new Object()
    if(url.startsWith('?')){
        url.substr(1).split('&').forEach(e=>{
            let a=e.split('=')
            if(a.length==2)
                arg[a[0]]=a[1]
        })
    }
    return arg
}
function addBooksInfo(data){
    data.forEach(e=>{
        addBooksElems(e.id,e.img,e.name,e.score,e.eval,e.writer,e.kind,e.intro)
    });
}
function addBooksElems(id,pic,name,score,evalNum,writer,kind,intro){
    let href=()=>location.href=`详细信息页?id=${id}`/*TODO*/
    let f=document.createElement('div')
    let p=document.createElement('img')
    let a=document.createElement('div')
    let n=document.createElement('a')
    let c=document.createElement('div')
    let s=document.createElement('a')
    let e=document.createElement('a')
    let w=document.createElement('a')
    let k=document.createElement('a')
    let i=document.createElement('a')
    let b=document.createElement('button')
    list.appendChild(f)
    f.appendChild(p)
    f.appendChild(a)
    f.appendChild(b)
    a.appendChild(n)
    a.appendChild(c)
    c.appendChild(s)
    c.appendChild(e)
    a.appendChild(w)
    a.appendChild(k)
    a.appendChild(i)
    p.src=pic
    n.innerText=name
    s.innerText=score
    c.style.backgroundPositionY=`${(Math.round(score)-10)*11}px`
    e.innerText=evalNum?`(${evalNum}人评价)`:'(暂时无人评价)'
    w.innerText=`作者：${writer}`
    k.innerText=`类型：${kind}`
    i.innerText=`简介：${intro}`
    b.innerText='书籍详情'
    p.onclick=href
    n.onclick=href
    b.onclick=href
}